//
//  ViewController.m
//  弹幕01
//
//  Created by My Mac on 2017/2/15.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "ViewController.h"
#import "UIColor+Random.h"
@interface ViewController ()
//文本输入框
@property(nonatomic,strong) UITextField *textField;
//发送按钮
@property(nonatomic,strong) UIButton *button;
//显示弹幕内容的lable
@property(nonatomic,strong) UILabel *lable;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化控件
    [self initView];
}

#pragma mark - 初始化控件
-(void)initView
{
    //文本输入框
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 240, 200, 40)];
    //文本框的提示文字
    _textField.placeholder = @"可输入15个文字";
    //文本框的边框类型
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    //将控件添加到视图上
    [self.view addSubview:_textField];
    //文本框的响应事件,限制输入的字数
    [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    //发送按钮
    _button = [[UIButton alloc]initWithFrame:CGRectMake(320, 240, 100, 40)];
    //设置按钮的标题
    [_button setTitle:@"发送" forState:UIControlStateNormal];
    //设置按钮的标题颜色
    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //设置按钮的背景色
    _button.backgroundColor = [UIColor blueColor];
    //将控件添加到视图上
    [self.view addSubview:_button];
    //按钮的响应事件
    [_button addTarget:self action:@selector(addLable) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 设置文本框的字数限制
- (void)textFieldDidChange:(UITextField *)textField
{
    //设置文本输入字符的最大值
    NSInteger maxString = 15;
    //获取文本信息
    NSString *textString = textField.text;
    //简体中文输入,包括简体拼音,健体五笔,简体手写
    UITextRange *textRange = [textField markedTextRange];
    //获取高亮部分
    UITextPosition *textPosition = [textField positionFromPosition:textRange.start offset:0];
    // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
    if (!textPosition){
        if (textString.length > maxString){
            //中文和表情存在问题，需要对此进行处理
            //字符的范围
             NSRange range = [textString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxString)];
            //设置文本输入框的范围
             textField.text = [textString substringWithRange:range];
        }
    }
}

#pragma mark - 弹幕Lable的添加
-(void)addLable
{
    /*
     添加一个lable,Lable的位置设定:
     X: 弹幕是从最右侧移向最右侧 所以X坐标为最右侧
     Y: 在屏幕的高度中随机的一个数值
     W: 自己设定
     H: 自己设定
     */
    //主屏幕的宽
    NSInteger width = [UIScreen mainScreen].bounds.size.width;
    //主屏幕的高
    NSInteger height = [UIScreen mainScreen].bounds.size.height - 50; //减50 是减去Lable本身的高度
    // rand()%height: 在height中随机取一个数值
    _lable = [[UILabel alloc]initWithFrame:CGRectMake(width, rand()%height, 400, 50)];
    [self.view addSubview:_lable];
    
    //判断文本输入框的内容是否为空
    if ([_textField.text isEqualToString:@""]) {
        NSLog(@"输入的文本不能为空");
    } else {
        //获取输入的内容
        NSString *showText = _textField.text;
        //调用分类(分类很简单的,就是设置三原色)
        _lable.textColor = [UIColor randomColor];
        //设置Lable的字体大小
        _lable.font = [UIFont systemFontOfSize:24];
        //将内容赋值给lable
        _lable.text = showText;
        //移动文字
        [self moveLable:_lable];
    }
    //将文本框清空
    _textField.text = NULL;
}

#pragma mark - ****移动文字（Lable）****
-(void)moveLable:(UILabel *)lable
{
    //视图动画效果
    [UIView animateWithDuration:10 animations:^{
        //单独这一条语句无效果
        /*
         弹幕的原理是将Lable从最右侧移至最左侧,改变的是X的值,其他的值都不改变,所以从新设置Lable的X值就行了
         */
        lable.frame = CGRectMake(0 - lable.frame.size.width, lable.frame.origin.y, lable.frame.size.width, lable.frame.size.height);
    } completion:^(BOOL finished){
        //移动结束之后,从父视图中移除lable
        [lable removeFromSuperview];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
